<template>
  <scroll-view scroll-y class="login-container">
    <!-- 头部 -->
    <view class="header">
      <text>用户注册</text>
    </view>
    
    <!-- 内容区域 -->
    <view class="content">
      <!-- 登录表单 -->
      <view class="form-group">
        <label for="username">用户名</label>
        <view class="input-wrapper">
          <i class="fas fa-user"></i>
          <input 
            type="text" 
            id="username" 
            placeholder="请输入用户名" 
            v-model="loginForm.username"
          >
        </view>
      </view>
      
	 <view class="form-group">
	    <label for="password">年龄</label>
	    <view class="input-wrapper">
	      <i class="fas fa-lock"></i>
	      <input 
	        type="number" 
	        id="age" 
	        placeholder="请输入年龄" 
	        v-model="loginForm.age"
	      >
	    </view>
	  </view>
	  
      <view class="form-group">
        <label for="password">学校</label>
        <view class="input-wrapper">
          <i class="fas fa-lock"></i>
          <input 
            type="text" 
            id="school" 
            placeholder="请输入学校" 
            v-model="loginForm.school"
          >
        </view>
		</view>
     
	  <view class="form-group">
        <label for="password">密码</label>
        <view class="input-wrapper">
          <i class="fas fa-lock"></i>
          <input 
		  :password = "showpasswordflag"
		  @click="showpassword"
            type="password" 
            id="password" 
            placeholder="请输入密码" 
            v-model="loginForm.password"
          >
        </view>
      </view>
	  
	  <view class="form-group">
        <label for="password">确认密码</label>
        <view class="input-wrapper">
          <i class="fas fa-lock"></i>
          <input 
		  :password="showsurepasswordflag"
			@click="showsurepassword"
            type="password" 
            id="surepassword" 
            placeholder="请确认密码" 
            v-model="loginForm.surepassword"
          >
        </view>
      </view>
      
      <button class="btn-login" @click="handleregin">
        <span v-if="!loading">注册</span>
        <view v-else class="loading-spinner"></view>
      </button>
      
      
      <!-- 请求结果展示 -->
      <view class="result-container" v-if="requestResult">
        <view class="result-title">
          <span>请求结果</span>
          <span v-if="requestResult.success" class="success-badge">
            <i class="fas fa-check-circle"></i> 请求成功
          </span>
          <span v-else class="error-badge">
            <i class="fas fa-exclamation-circle"></i> 请求失败
          </span>
        </view>
        <view class="result-content">
          {{ formatResult(requestResult) }}
        </view>
      </view>
    </view>
    
    <!-- 页脚 -->
    <view class="footer">
		<view>
			<text>博客管理系统  <a href="#">© 2025</a></text>
			<br/>
      <text> 隐私政策 | 服务条款</text>
		</view>
      
    </view>
    
    <!-- 使用请求组件 -->
    <RequestComponent 
      ref="requestComponent"
      :url="myrequest.baseurl+'/user/register'"
      method="POST"
      :request-data="loginForm"
    />
  </scroll-view>
</template>

<script>
import RequestComponent from '@/component/RequestCom.vue';
	import myrequest from "@/untils/request.js"
export default {
  components: {
    RequestComponent
  },
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
		age:"",
		school:"",
		surepassword:""
      },
      loading: false,
      requestResult: null,    //接收请求结果
	  showpasswordflag:true,
	  showsurepasswordflag:true
    };
  },
  methods: {
    // 处理登录逻辑
    handleregin() {       
      if (!this.loginForm.username || !this.loginForm.password || !this.loginForm.surepassword
	   || !this.loginForm.age || !this.loginForm.school) {   //其中有一个为空
        this.requestResult = {       //填写的返回结果
          success: false,
          message: '请输入用户名和密码并确认',
          timestamp: new Date().toISOString()     //时间
        };
		uni.showToast({
			title:"请填写全部信息",
			icon:'error'
		})
        return;
      }
      
	  if(this.loginForm.password !== this.loginForm.surepassword){
		  this.requestResult = {       //填写的返回结果
		    success: false,
		    message: '两次密码不一致',
		    timestamp: new Date().toISOString()     //时间
		  };
		  uni.showToast({
		  	title:"两次密码不一致",
		  	icon:'error'
		  })
		  return;
	  }

	  if(parseInt(this.loginForm.age).toString() === "NaN"){
		   uni.showToast({
		  	title:"age必须为数字！！",
		  	icon:'error'
		  })
		  return
	  }
	  //都填写了 并确认
      this.loading = true;      //正在加载
      this.requestResult = null;    // 没有返回结果
      
      // 使用请求组件发送登录请求
      const requestComponent = this.$refs.requestComponent;    //获取组件
      
	  uni.request({
			url:myrequest.baseurl+"/user/register?username="+this.loginForm.username +
			"&password="+this.loginForm.password + "&age="+this.loginForm.age+"&school="+this.loginForm.school,
			data:"",
			method:"POST"
		}).then(res=>{
			if(res.statusCode === 200){
				if(res.data.code===0){
					uni.showToast({
						title:res.data.message,
						icon:'success'
					})
					// uni.$emit("registersuccess",{username:this.loginForm.username,password:this.loginForm.password})
					setTimeout(()=>{
						uni.navigateTo({
							url:"/pages/router/login/login"
						})
					},500)
					
				}else{
					uni.showToast({
						title:res.data.message,
						icon:'error'
					})
				}
			}
			this.loading = false;    
		}).catch(()=>{
			uni.showToast({
				title:error.errMsg || '网络请求失败',
				icon:'error'
			})
			this.requestResult = {
			  success: false,
			  status: error.statusCode || 500,
			  message:  '网络问题或者服务器暂未开放',
			  timestamp: new Date().toISOString()
			};
			this.loading = false;
		})
    
	},
    
    // 格式化请求结果显示
    formatResult(result) {
      if (result.success) {
        return JSON.stringify(result.data, null, 2);
      } else {
        return `错误代码: ${result.status || 500}\n错误信息: ${result.message || '未知错误'}`;
      }
    },
	showpassword(){
		this.showpasswordflag = !this.showpasswordflag
	},
	showsurepassword(){
		this.showsurepasswordflag = !this.showsurepasswordflag
	}
	
  },
  onLoad() {
  	
  }
};
</script>

<style scoped>
/* * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
} */

.login-container {
  width: 100%;
  height: 100vh;
  max-width: 500px;
  margin: 0 auto;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.header {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 46px;
  background: linear-gradient(to right, #4776E6, #8E54E9);
  color: white;
  padding: 12px 8px;
  text-align: center;
}

.header text {
  font-size: 25px;
  font-weight: 600;
  margin-bottom: 8px;
}

.header text {
  opacity: 0.9;
  font-size: 16px;
}

.content {
  padding: 30px 30px 10px 30px;
}

.form-group {
  margin-bottom: 25px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 15px;
}

.input-wrapper {
  position: relative;
}

.input-wrapper i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #777;
  font-size: 18px;
}

.input-wrapper input {
  width: 80%;
  padding: 10px 10px 10px 40px;
  border: 2px solid #e1e5f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s;
  outline: none;
}

.input-wrapper input:focus {
  border-color: #4776E6;
  box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.2);
}

.btn-login {
  background: linear-gradient(to right, #4776E6, #8E54E9);
  color: white;
  border: none;
  border-radius: 12px;
  padding: 5px;
  font-size: 18px;
  font-weight: 400;
  width: 80%;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(71, 118, 230, 0.3);
}

.btn-login:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 20px rgba(71, 118, 230, 0.4);
}

.btn-login:active {
  transform: translateY(0) scale(0.95);
}

.viewider {
  display: flex;
  align-items: center;
  margin: 25px 0;
}

.viewider span {
  padding: 0 15px;
  color: #777;
  font-size: 14px;
}

.viewider::before,
.viewider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e1e5f0;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 25px;
}

.social-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
}

.social-btn:hover {
  transform: translateY(-3px);
}

.social-btn.wechat {
  background: linear-gradient(to right, #09BB07, #2ADD1C);
}

.social-btn.alipay {
 background: linear-gradient(to right, #1677FF, #1E90FF);
    /* background: linear-gradient(to right, #4776E6, #8E54E9); */
	font-size: 1em;
}

.social-btn.apple {
  background: linear-gradient(to right, #000, #333);
}

.footer {
	background-color: #f8f9ff;
	width: 90%;
	position: relative;
	bottom: 0;
	/* display: flex; 
	justify-content: center; 
	align-items: center; */
  text-align: center;
  padding: 0 20px 20px 20px ;
  color: #777;
  font-size: 14px;
  border-top: 1px solid #eee;
}
.footer > view{
	/* width: 80%; */
	/* background-color: #4776E6; */
	text-align: center;
}

.footer a {
  color: #4776E6;
  text-decoration: none;
  font-weight: 500;
}

.result-container {
  background: #f8f9ff;
  border-radius: 12px;
  padding: 20px;
  margin-top: 25px;
  border: 1px solid #e1e5f0;
  max-height: 300px;
  overflow-y: auto;
}

.result-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 15px;
  color: #4776E6;
  display: flex;
  align-items: center;
  gap: 10px;
}

.result-content {
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  white-space: pre-wrap;
  word-break: break-all;
}

.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.success-badge {
  display: inline-flex;
  align-items: center;
  background: #e6f7ee;
  color: #09bb07;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.error-badge {
  display: inline-flex;
  align-items: center;
  background: #fdeeee;
  color: #f44336;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}
</style>